package main

import (
	"PrintTrnsferServer/common"
	"PrintTrnsferServer/server"
	"log"
	"strings"
)

//1620962512634249218
// func getData(param string) string {
// 	result := dao.DbManager.FindOrder(param)
// 	data, _ := json.Marshal(result)
// 	return string(data)
// }

func main() {
	(&server.SocketServer{
		Network:             "tcp4",
		Address:             common.Config.Server,
		Clients:             make(map[string]*server.TcpConn),
		ClientTimeOutSecond: 50,
		MaxClientNum:        200,
		OnError: func(msg string, err error) {
			log.Println(msg, err)
		},
		OnStart: func(server *server.SocketServer) {
			log.Println("启动OK")
		},
		OnClientConnect: func(client *server.TcpConn) {
			log.Println("客户端连接成功:", client.Id)
		},
		OnClientClose: func(client *server.TcpConn) {
			log.Println("客户端连接关闭:", client.Id)
		},
		OnMessage: func(client *server.TcpConn, msg string) {
			msg = strings.Trim(msg, "\n")
			//log.Println(msg)
			if strings.Contains(msg, "&") {
				msgs := strings.Split(msg, "&")
				if len(msgs) == 3 {
					switch msgs[0] {
					case "cmd":
						if msgs[1] == "show" {
							msgs2 := "all,printer,pda"
							if strings.Contains(msgs2, msgs[2]) {
								text := client.Server.ShowClients(msgs[2])
								client.Connect.Write([]byte(text))
							}
						}
						if msgs[1] == "sign" && msgs[2] != "" {
							client.Server.SignPrinter(client, msgs[2])
						}
					case "print":
						target, isOk := client.Server.Clients[msgs[1]]
						if isOk {
							target.Connect.Write([]byte(msgs[2]))
						}
					}
				}
			}
			//client.Connect.Write([]byte("ok:" + msg))
		},
	}).Start()
}
